package com.rerun.phone.park;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.rerun.common.anno.ResponseExceptionReturn;
import com.rerun.common.bean.Rest;
import com.rerun.common.controller.SuperController;
import com.rerun.entity.ChatRecords;
import com.rerun.enums.ChatRecordsStatus;
import com.rerun.service.IChatRecordsService;
import com.rerun.service.IChatService;
import com.rerun.vo.memberUser.ChatRecordsSearchVo;
import com.rerun.vo.memberUser.ChatRecordsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Api(value = "园长端-聊天记录", description = "园长端-聊天记录")
@RestController
@RequestMapping("/parkApi/chat")
public class ParkChatRecordController extends SuperController {
	@Autowired
	IChatRecordsService chatRecordsService;
	@Autowired
    private IChatService iChatService;
	/**
	 * 
	 * @return
	 */
	@ApiOperation(value = "添加聊天记录", httpMethod = "POST", notes = "添加聊天记录")
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ApiImplicitParams({ @ApiImplicitParam(name = "memberId", required = true, value = "用户ID", paramType = "query"),
			@ApiImplicitParam(name = "parkId", required = true, value = "园长ID", paramType = "query"), })
	public Rest add(@ModelAttribute ChatRecords records) {
		try {
			records.setUserStatus(ChatRecordsStatus.IS_SHOW.getKey());
			records.setParkStatus(ChatRecordsStatus.IS_SHOW.getKey());
			records.setLastTime(new Date());
			records.setCreateTime(new Date());
			chatRecordsService.addChatRecords(records, false);
			return Rest.ok("添加成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Rest.failure("添加失败");
	}

	@ApiOperation(value = "聊天列表", httpMethod = "GET", notes = "聊天记录列表")
	@RequestMapping(value = "/list/{parkId}/{pageNumber}", method = RequestMethod.GET)
	public Rest list(@PathVariable String parkId, @PathVariable Integer pageNumber) {
		try {
			Page<ChatRecordsVo> page = getPage(pageNumber);
			ChatRecordsSearchVo chatRecords = new ChatRecordsSearchVo();
			chatRecords.setParkId(parkId);
			chatRecords.setParkStatus(ChatRecordsStatus.IS_SHOW.getKey());
			page = chatRecordsService.selectParkRecordsList(chatRecords, page);
			return Rest.okData(page.getRecords());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Rest.failure("获取列表失败");
	}

	@ApiOperation(value = "删除聊天记录", httpMethod = "GET", notes = "删除聊天记录")
	@RequestMapping(value = "/delete/{chatId}", method = RequestMethod.GET)
	public Rest delete(@PathVariable String chatId) {
		try {
			ChatRecords records = new ChatRecords();
			records.setParkStatus(ChatRecordsStatus.IS_DELETE.getKey());
			EntityWrapper<ChatRecords> wrapper = new EntityWrapper<>();
			wrapper.eq("chatId", chatId);
			chatRecordsService.update(records, wrapper);
			return Rest.ok("删除成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Rest.failure("删除失败");
	}
	
	@ApiOperation(value = "园长端-园长聊天帐号获取", notes = "聊天帐号获取", httpMethod = "GET")
	@RequestMapping(value = "/parkInfo/{parkId}", method = RequestMethod.GET)
	@ResponseExceptionReturn
	public Rest getOwnChatAccount(@PathVariable("parkId") long parkId) {

		return Rest.okData(iChatService.getParkChatUser(parkId));
	}
	@ApiOperation(value = "园长端-根据memberId获取求职者聊天帐号", notes = "根据memberId获取求职者环信帐号", httpMethod = "GET")
	@RequestMapping(value = "/userInfo/{memberId}", method = RequestMethod.GET)
	@ResponseExceptionReturn
	public Rest getApplyJobUser(@PathVariable("memberId") String memberId) {
		return Rest.okData(iChatService.getMemberChatUser(memberId));
	}
}
